package com.cjm.one.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjm.one.model.system.dto.DeptDto;
import com.cjm.one.model.system.entity.SysUserInfo;
import com.cjm.one.model.system.vo.SysDeptAllVo;
import com.cjm.one.system.mapper.SysDeptMapper;
import com.cjm.one.system.mapper.SysUserInfoMapper;
import com.cjm.one.system.service.SysDeptService;
import com.cjm.one.model.system.entity.SysDept;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 29289
* @description 针对表【sys_dept(部门表)】的数据库操作Service实现
* @createDate 2024-09-05 16:21:13
*/
@Service
@Slf4j
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept>
    implements SysDeptService {
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private SysDeptMapper sysDeptMapper;

    @Resource
    private SysUserInfoMapper sysUserInfoMapper;
    //获取所有部门列表
    @Override
    public List<SysDeptAllVo> getAllList(String deptName, String keyWord) {
//    TODO:超级管理员查询全部     redisTemplate.opsForValue().get()
        LambdaQueryWrapper<SysDept> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(deptName != null, SysDept::getName, deptName);
        queryWrapper.like(keyWord != null, SysDept::getAdmin, keyWord);
        List<SysDept> sysDepts =  sysDeptMapper.selectList(queryWrapper);
        List<SysDeptAllVo> list = treeDept(sysDepts, 0L);
        log.info("获取所有部门列表:{}",list);
        return list;

    }

    @Override
    public Page<SysDept> getUserDeptList(Page page,DeptDto deptDto) {
        LambdaQueryWrapper<SysDept> wrapper = new LambdaQueryWrapper<>();
        if (deptDto.getName() != null) {
            wrapper.like(SysDept::getName, deptDto.getName());
        }
        if (deptDto.getAdmin() != null) {
            wrapper.eq(SysDept::getAdmin, deptDto.getAdmin());
        }
        if (deptDto.getParentId() != null) {
            wrapper.like(SysDept::getAncestors, deptDto.getParentId());
        }
        wrapper.orderByAsc(SysDept::getDeptSort);


        return sysDeptMapper.selectPage(page, wrapper);
    }

    @Override
    public Boolean removeDeptById(Long id) {
        LambdaQueryWrapper<SysUserInfo> sysUserInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysUserInfoLambdaQueryWrapper.eq(SysUserInfo::getDeptId, id);
        SysUserInfo sysUserInfo = sysUserInfoMapper.selectOne(sysUserInfoLambdaQueryWrapper);
        if (sysUserInfo != null) {
            return false;
        }
        int i = sysDeptMapper.deleteById(id);
        return i>0;
    }

    /**
     * @description: 递归生成部门树
     * @author: cjm
     * @date: 2024/10/9 22:13
     * @param: [list, parentId]
     * @return: java.util.List<com.cjm.one.model.system.vo.SysDeptAllVo>
     **/
    public List<SysDeptAllVo> treeDept (List<SysDept> list,Long parentId) {
        if (list == null || list.size() == 0) {
            return null;
        }
        List<SysDeptAllVo> data = new ArrayList<>();
        for (SysDept sysDept : list) {
            if (sysDept.getParentId().equals(parentId)) {
                SysDeptAllVo sysDeptAllVo = new SysDeptAllVo();
                BeanUtils.copyProperties(sysDept, sysDeptAllVo);
                sysDeptAllVo.setChildren(treeDept(list, sysDept.getId()));
                data.add(sysDeptAllVo);
            }
        }

        return data;
    }
}




